import Vue from 'vue';
import Vuex from 'vuex';
import * as echarts from 'echarts'; // 引入 echarts 库
Vue.use(Vuex); // 安装 Vuex 插件

// 创建 Vuex 实例并导出
export default new Vuex.Store({
  // 定义状态
  state: {
    isAuthenticated: false, // 用户是否已经认证
    role: '', // 用户角色
    user: null, // 当前用户信息
    users: [], // 用户列表
    orders: [], // 订单列表
    products: [], // 商品列表
    permissions: [], // 用户权限列表
    salesData: {}, // 销售数据
    stockData: {}, // 存量数据
    avatarUrl: '', // 头像 URL
    isNightMode: false // 夜间模式状态
  },
  // 定义 mutations，用于同步修改状态
  mutations: {
    // 设置用户认证状态和角色
    setAuthentication(state, payload) {
      state.isAuthenticated = payload.isAuthenticated;
      state.role = payload.role;
    },
    // 清除用户认证状态和角色
    clearAuthentication(state) {
      state.isAuthenticated = false;
      state.role = '';
    },
    // 设置当前用户信息
    setUser(state, payload) {
      state.user = payload;
    },
    // 设置用户列表
    setUsers(state, payload) {
      state.users = payload;
    },
    // 添加用户到列表
    addUser(state, payload) {
      state.users.push(payload);
    },
    // 设置头像 URL
    setAvatarUrl(state, url) {
      state.avatarUrl = url;
    },
    // 更新用户信息
    updateUser(state, { index, user }) {
      Vue.set(state.users, index, user);
    },
    // 从列表中删除用户
    deleteUser(state, index) {
      state.users.splice(index, 1);
    },
    // 设置订单列表
    setOrders(state, payload) {
      state.orders = payload;
    },
    // 添加订单到列表
    addOrder(state, payload) {
      state.orders.push(payload);
    },
    // 更新订单信息
    updateOrder(state, { index, order }) {
      Vue.set(state.orders, index, order);
    },
    // 从订单列表中删除订单
    deleteOrder(state, index) {
      state.orders.splice(index, 1);
    },
    // 设置商品列表
    setProducts(state, payload) {
      state.products = payload;
    },
    // 添加商品到列表
    addProduct(state, payload) {
      state.products.push(payload);
    },
    // 更新商品信息
    updateProduct(state, { index, product }) {
      Vue.set(state.products, index, product);
    },
    // 从商品列表中删除商品
    deleteProduct(state, index) {
      state.products.splice(index, 1);
    },
    // 设置用户权限列表
    setPermissions(state, payload) {
      state.permissions = payload;
    },
    // 设置销售数据
    setSalesData(state, payload) {
      state.salesData = payload;
    },
    // 设置存量数据
    setStockData(state, payload) {
      state.stockData = payload;
    },
    // 切换夜间模式状态
    toggleNightMode(state) {
      state.isNightMode = !state.isNightMode;
    }
  },
  // 定义 actions，用于异步操作和提交 mutations
  actions: {
    // 登录动作
    login({ commit }, user) {
      commit('setAuthentication', { isAuthenticated: true, role: user.role });
      commit('setUser', user);
    },
    // 注销动作
    logout({ commit }) {
      commit('clearAuthentication');
      commit('setUser', null);
      commit('setPermissions', []);
    },
    // 设置头像 URL
    setAvatarUrl({ commit }, url) {
      commit('setAvatarUrl', url);
    },
    // 模拟获取用户列表
    fetchUsers({ commit }) {
      const users = [
        { id: 1, username: '依古比古', email: 'user1@garden.com' },
        { id: 2, username: '唔西迪西', email: 'user2@garden.com' },
        { id: 3, username: '玛卡巴卡', email: 'user3@garden.com' },
        { id: 4, username: '汤布利卜', email: 'user4@garden.com' },
        { id: 5, username: '小点点', email: 'user5@garden.com' },
        { id: 6, username: '叮叮车', email: 'user6@garden.com' },
      ];
      commit('setUsers', users);
    },
    // 添加用户
    addUser({ commit, state }, user) {
      const newId = state.users.length ? Math.max(...state.users.map(u => u.id)) + 1 : 1;
      const newUser = { ...user, id: newId };
      commit('addUser', newUser);
    },
    // 更新用户信息
    updateUser({ commit }, payload) {
      commit('updateUser', payload);
    },
    // 删除用户
    deleteUser({ commit }, index) {
      commit('deleteUser', index);
    },
    // 模拟获取订单列表
    fetchOrders({ commit }) {
      const orders = [
        { id: 1, customerName: '虹猫', totalAmount: 100, status: 'pending' },
        { id: 2, customerName: '蓝兔', totalAmount: 200, status: 'shipped' },
        { id: 3, customerName: '莎丽', totalAmount: 170, status: 'pending' },
        { id: 4, customerName: '逗逗', totalAmount: 240, status: 'shipped' },
        { id: 5, customerName: '大奔', totalAmount: 120, status: 'pending' },
        { id: 6, customerName: '跳跳', totalAmount: 350, status: 'shipped' },
      ];
      commit('setOrders', orders);
    },
    // 添加订单
    async addOrder({ commit, state }, order) {
      const newId = state.orders.length ? Math.max(...state.orders.map(o => o.id)) + 1 : 1;
      const totalAmount = parseFloat(order.totalAmount);
      if (isNaN(totalAmount)) {
        throw new Error('总金额必须是数字');
      }
      const newOrder = { ...order, id: newId, totalAmount };
      commit('addOrder', newOrder);
    },
    // 更新订单信息
    updateOrder({ commit }, payload) {
      commit('updateOrder', payload);
    },
    // 删除订单
    deleteOrder({ commit }, index) {
      commit('deleteOrder', index);
    },
    // 模拟获取商品列表
    fetchProducts({ commit }) {
      const products = [
        { id: 1, name: '2090', price: 100, stock: 10 },
        { id: 2, name: '2090Ti', price: 200, stock: 20 },
        { id: 3, name: '3090', price: 300, stock: 5 },
        { id: 4, name: '3090Ti', price: 400, stock: 4 },
        { id: 5, name: '4090', price: 500, stock: 3 },
        { id: 6, name: '4090Ti', price: 600, stock: 20 }
      ];
      commit('setProducts', products);
    },
    // 添加商品
    addProduct({ commit, state }, product) {
      const newId = state.products.length ? Math.max(...state.products.map(p => p.id)) + 1 : 1;
      const newProduct = { ...product, id: newId };
      commit('addProduct', newProduct);
    },
    // 更新商品信息
    updateProduct({ commit }, payload) {
      commit('updateProduct', payload);
    },
    // 删除商品
    deleteProduct({ commit }, index) {
      commit('deleteProduct', index);
    },
    // 模拟获取销售数据
    fetchSalesData({ commit }) {
      const salesData = {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [
          {
            label: 'Sales',
            backgroundColor: '#f87979',
            data: [40, 20, 12, 39, 10, 40, 39]
          }
        ]
      };
      commit('setSalesData', salesData);
    },
    // 模拟获取存量数据
    fetchStockData({ commit }) {
      const stockData = {
        labels: ['Product 1', 'Product 2', 'Product 3', 'Product 4'],
        datasets: [
          {
            label: 'Stock',
            backgroundColor: '#34bfa3',
            data: [100, 200, 150, 50]
          }
        ]
      };
      commit('setStockData', stockData);
    },
    // 切换夜间模式
    toggleNightMode({ commit }) {
      commit('toggleNightMode');
    }
  }
});
